package com.zjitc.book.controller;

import cn.hutool.core.date.DateUtil;
import cn.hutool.jwt.JWTUtil;
import com.zjitc.book.common.result.R;
import com.zjitc.book.dto.UserDto;
import com.zjitc.book.entity.MyUserDetails;
import com.zjitc.book.entity.Permissions;
import com.zjitc.book.mapper.UserMapper;
import com.zjitc.book.service.UserInfoService;
import com.zjitc.book.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

//@CrossOrigin
@RestController
@RequestMapping("/api")
public class UserController {
    public static final String key="123123123123123123";
    @Autowired
    UserMapper userMapper;
    @Autowired
    AuthenticationManager authenticationManager;
    @Autowired
    BCryptPasswordEncoder bCryptPasswordEncoder;
    @Autowired
    StringRedisTemplate stringRedisTemplate;
    @Autowired
    UserInfoService userService;

    @PostMapping("/auth/login")
    public R<Map> login(@RequestBody UserDto user){//Entity和DTO的区别
        System.out.println(user);
        //1.借助authenticationManager来完成认证
        UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword());
        Authentication authentication = authenticationManager.authenticate(auth);

        //2.从authentication获取用户信息
        MyUserDetails principal = (MyUserDetails)authentication.getPrincipal();
        Integer userId=principal.getUser().getId();
        String username=principal.getUser().getUsername();

        List<String> authorities = principal.getAuthors();
        //2.1 把登陆的用户权限存入redis
        stringRedisTemplate.opsForSet().add("user.perms:"+userId,authorities.toArray(new String[0]));
//3.用hutool生成token
        Map<String,Object> map=new HashMap<>();
        map.put("userid",userId);
        map.put("username",username);
        map.put("exp", DateUtil.currentSeconds()+60+60);
        String token = JWTUtil.createToken(map, key.getBytes());
//4.返回响应
        Map<String,Object> res=new HashMap<>();
        res.put("token",token);
        res.put("userid",userId);
        res.put("username",username);

        return R.success(res);
    }
    @PutMapping("/edit")
    public void edit(@RequestBody UserDto user){
        String encode = bCryptPasswordEncoder.encode(user.getPassword());
        user.setPassword(encode);
        userMapper.insertUser(user);
    }

    @GetMapping("/user/menu")
    public List<Permissions> getMenu(){
        return userService.getMenu();
    }
}
